package cn.cs.message.consumer;

import java.io.IOException;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeoutException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.cs.message.ConnectionManage;
import cn.cs.message.MessageClient;
import cn.cs.message.ReconnectHandler;
import cn.cs.message.support.MessageNameService;

/**消息接消费者，需要继承该类实现消息处理方法handlMessage(...)
 * @version 1.0.0*/
public abstract class MessageConsumer extends MessageClient 
{
	private static final Logger logger=LoggerFactory.getLogger(MessageConsumer.class);
	
	/**在下次返回ack时，rmq服务器发送的最大个数*/
	protected int qos=50;	
	protected int threadPoolSize=10;
	
	public void start() throws IOException, TimeoutException, URISyntaxException
	{
		try{
			connectManage=new ConnectionManage(connectUri,threadPoolSize,getReconnectHandler());
		} catch (IOException e) {
			logger.error("MQ连接失败,请检查MQ链接配置信息",e);
			return;
		} catch (KeyManagementException e) {
			logger.error("MQ连接失败,请检查MQ链接配置信息",e);
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			logger.error("MQ连接失败,请检查MQ链接配置信息",e);
			e.printStackTrace();
		}
		messageNameService=new MessageNameService();
		messageNameService.setAtoB(direction);
		doStart();
	}
	
	public abstract ReconnectHandler getReconnectHandler();
	protected abstract void doStart() throws IOException, TimeoutException;
	protected abstract void doStop() throws IOException, TimeoutException;
	
	public void stop() throws IOException, TimeoutException
	{
		doStop();
		connectManage.shutdown();
	}

	public int getQos() {
		return qos;
	}

	public void setQos(int qos) {
		this.qos = qos;
	}

	public int getThreadPoolSize() {
		return threadPoolSize;
	}

	public void setThreadPoolSize(int threadPoolSize) {
		this.threadPoolSize = threadPoolSize;
	}

}
